/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2018年6月3日
 * <修改描述:>
 */
package com.tx.core.mybatis.autosqlmap;

import com.tx.core.mybatis.support.MyBatisDaoSupport;
import com.tx.core.util.TableInfoUtils;
import com.tx.core.util.TableInfoUtils.TableColumnInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * 实体持久层注册器<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2018年6月3日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@SuppressWarnings("rawtypes")
public class AutoSqlMapEntityDaoFactory implements FactoryBean<AutoSqlMapEntityDao>, InitializingBean {

    protected Logger logger = LoggerFactory.getLogger(AutoSqlMapEntityDaoFactory.class);

    private Class<?> beanType;

    private DefaultAutoSqlMapEntityDaoImpl entityDao;

    /**
     * <默认构造函数>
     */
    public AutoSqlMapEntityDaoFactory(Class<?> beanType, MyBatisDaoSupport myBatisDaoSupport) {
        super();
        this.beanType = beanType;
    }

    /**
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    @Override
    public void afterPropertiesSet() {
        //构建Dao
        TableColumnInfo pk = TableInfoUtils.parsePKTableColumn(beanType);
        DefaultAutoSqlMapEntityDaoImpl ed = new DefaultAutoSqlMapEntityDaoImpl(this.beanType, pk.getPropertyType());
        ed.afterPropertiesSet();
        this.entityDao = ed;

        logger.info("构建实体自动持久层，beanType:{},namespace:{}", this.beanType.getName(), this.entityDao.getAssistant().getNamespace());
    }

    /**
     * @return
     */
    @Override
    public boolean isSingleton() {
        return true;
    }

    /**
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return AutoSqlMapEntityDao.class;
    }

    /**
     * @return
     * @throws Exception
     */
    @Override
    public AutoSqlMapEntityDao getObject() throws Exception {
        return this.entityDao;
    }
}
